package com.atguigu.srb.core.service.impl;

import com.atguigu.srb.base.result.ResponseEnum;
import com.atguigu.srb.base.util.JwtUtils;
import com.atguigu.srb.base.util.SrbAssert;
import com.atguigu.srb.core.pojo.entity.IntegralGrade;
import com.atguigu.srb.core.mapper.IntegralGradeMapper;
import com.atguigu.srb.core.pojo.entity.UserInfo;
import com.atguigu.srb.core.service.IntegralGradeService;
import com.atguigu.srb.core.service.UserInfoService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * <p>
 * 积分等级表 服务实现类
 * </p>
 *
 * @author muzhao
 * @since 2024-08-19
 */
@Service
public class IntegralGradeServiceImpl extends ServiceImpl<IntegralGradeMapper, IntegralGrade> implements IntegralGradeService {
    @Resource
    UserInfoService userInfoService;

    @Override
    public BigDecimal getBorrowAmountByUserId(String token) {
        //获取用户id
        Long userId = JwtUtils.getUserId(token);
        //查询积分
        UserInfo userInfo = userInfoService.getById(userId);
        Integer integral = userInfo.getIntegral();
        //根据积分查询积分等级对象
        //select * from integral_grade t1 where t1.integral_start <= 60 and t1.integral_end >= 60;
        IntegralGrade integralGrade = this.getOne(Wrappers.lambdaQuery(IntegralGrade.class)
                .le(IntegralGrade::getIntegralStart, integral)
                .ge(IntegralGrade::getIntegralEnd, integral)
                .last("limit 1"));
        SrbAssert.assertNotTrue(integralGrade==null, ResponseEnum.ERROR);
        return integralGrade.getBorrowAmount();

    }
}
